package com.ruoyi.rpt.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.rpt.domain.RptCategory;
import com.ruoyi.rpt.service.IRptCategoryService;

/**
 * 报表分类Controller
 */
@RestController
@RequestMapping("/rpt/category")
public class RptCategoryController extends BaseController {
    
    @Autowired
    private IRptCategoryService rptCategoryService;
    
    /**
     * 查询报表分类列表
     */
    // @RequiresPermissions("rpt:category:list")
    @GetMapping("/list")
    public AjaxResult list(RptCategory rptCategory) {
        List<RptCategory> categories = rptCategoryService.selectRptCategoryList(rptCategory);
        return success(categories);
    }
    
    /**
     * 获取报表分类详细信息
     */
    // @RequiresPermissions("rpt:category:query")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId) {
        return success(rptCategoryService.selectRptCategoryByCategoryId(categoryId));
    }
    
    /**
     * 获取报表分类树形结构
     */
    @GetMapping("/tree")
    public AjaxResult tree() {
        List<RptCategory> categories = rptCategoryService.selectRptCategoryList(new RptCategory());
        return success(rptCategoryService.buildCategoryTree(categories));
    }
    
    /**
     * 获取报表分类下拉树形结构
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect() {
        List<RptCategory> categories = rptCategoryService.selectRptCategoryList(new RptCategory());
        return success(rptCategoryService.buildCategoryTreeSelect(categories));
    }
    
    /**
     * 新增报表分类
     */
    // @RequiresPermissions("rpt:category:add")
    @Log(title = "报表分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RptCategory rptCategory) {
        return toAjax(rptCategoryService.insertRptCategory(rptCategory));
    }
    
    /**
     * 修改报表分类
     */
    // @RequiresPermissions("rpt:category:edit")
    @Log(title = "报表分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RptCategory rptCategory) {
        return toAjax(rptCategoryService.updateRptCategory(rptCategory));
    }
    
    /**
     * 删除报表分类
     */
    // @RequiresPermissions("rpt:category:remove")
    @Log(title = "报表分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        return toAjax(rptCategoryService.deleteRptCategoryByCategoryIds(categoryIds));
    }
    
    /**
     * 获取分类下的报表
     */
    // @RequiresPermissions("rpt:category:query")
    @GetMapping("/{categoryId}/reports")
    public AjaxResult reports(@PathVariable Long categoryId) {
        return success(rptCategoryService.selectReportsByCategory(categoryId));
    }
    
    /**
     * 报表关联分类
     */
    // @RequiresPermissions("rpt:category:relate")
    @Log(title = "报表分类关联", businessType = BusinessType.UPDATE)
    @PostMapping("/relate")
    public AjaxResult relate(@RequestBody Map<String, Object> params) {
        Long reportId = Long.valueOf(params.get("reportId").toString());
        Long[] categoryIds = (Long[]) params.get("categoryIds");
        return toAjax(rptCategoryService.relateReportToCategories(reportId, categoryIds));
    }
} 